package com.yj.spider.service;

import com.google.common.util.concurrent.AbstractExecutionThreadService;
import com.yj.spider.Manager.SpiderManager;
import org.springframework.beans.factory.annotation.Autowired;
import us.codecraft.webmagic.Spider;

/**
 * 爬虫业务类
 */
public class YjSpider extends AbstractExecutionThreadService {
    @Autowired
    SpiderManager spiderManager;

    Spider spider = null;

    @Override
    protected void run() throws Exception {
        if (spider==null){
            throw  new RuntimeException("spider is null,please set spider before run spider!");
        }
        spider.run();
    }

    /**
     * 注入Spider
     *
     * @param spider
     */
    public YjSpider setSpider(Spider spider) {
        this.spider = spider;
        return this;
    }

    public  YjSpider setSPiderListener(){
        return  this;
    }

    @Override
    protected void shutDown() throws Exception {
        super.shutDown();
        spider.stop();
    }

    /**
     * 暴露WebMagic Spider信息
     * @return
     */
    public Spider spider(){
        return spider;
    }
}
